/**

 * @Title: RegionServiceImpl.java

 * @Package com.yellowpage.ca.service

 * @Description: TODO

 * Copyright: Copyright (c) 2011 

 * Company:  北京亚信科技有限公司

 * 

 * @author Comsys-xuys

 * @date 2015-6-26 下午3:12:33

 * @version V1.0

 */

package com.yellowpage.ca.service;

import java.util.List;

import javax.sound.midi.SysexMessage;

import com.yellowpage.ca.dao.RegionDao;
import com.yellowpage.white.service.BaseServiceImpl;

/**
 * @ClassName: RegionServiceImpl
 * @Description: 根据条件获得地区信息
 * @author Comsys-xuys
 * @date 2015-6-26 下午3:12:33
 * 
 */

public class RegionServiceImpl extends BaseServiceImpl implements RegionService {

	private RegionDao regionDao;

	/*
	 * 
	 * <p>Title: getRegionListByUserFunction</p>
	 * 
	 * <p>根据用户id及功能获得地区信息 </p>
	 * 
	 * @param id
	 * 
	 * @param function
	 * 
	 * @return
	 * 
	 * @see
	 * com.yellowpage.ca.service.RegionService#getRegionListByUserFunction(java
	 * .lang.Long, java.lang.String)
	 */
	@Override
	public List getRegionListByUserFunction(Long user_id, String function_code) {
		List list = regionDao.getRegionListByUserFunction(user_id, function_code);
		return list;
	}

	/*
	 * 
	 * <p>Title: getRegionclauseByUserFunction</p> <p>根据用户id，权限功能，字段获取sql </p>
	 * 
	 * @param id
	 * 
	 * @param function
	 * 
	 * @param regionField
	 * 
	 * @return
	 * 
	 * @see
	 * com.yellowpage.ca.service.RegionService#getRegionclauseByUserFunction
	 * (java.lang.Long, java.lang.String, java.lang.String)
	 */

	@Override
	public String getRegionclauseByUserFunction(Long user_id, String function_code, String regionField) {
		List<String> list = regionDao.getRegionListByUserFunction(user_id, function_code);
		StringBuffer sql = new StringBuffer();
		if(list.size()>0){
			sql.append(" AND ( ");
			for(String r : list){
				sql.append(regionField+" like ");
				if(r.equals("000000")){
					r="______";
				}else if(r.matches("[0-9]{2}0000")){
					r = r.substring(0,2)+"____";
				}else if(r.matches("[0-9]{4}00")){
					r = r.substring(0,4)+"__";
				}
				sql.append("'").append(r).append("' OR ");
			}
			sql.delete(sql.lastIndexOf("OR"), sql.length());
			sql.append(")");
		}
			
		
		return sql.toString();
	}

	@Override
	public String getRegion(Long user_id, String function_code, String regionField) {
		List<String> list = regionDao.getRegionListByUserFunction(user_id, function_code);
		StringBuffer sql = new StringBuffer();
		if(list.size()>0){
			sql.append(" AND ( ");
			for(String r : list){
				sql.append(regionField+" like ");
				if(r.equals("000000")){
					r="__0000";
				}else{
					r = r.substring(0,2)+"0000";
				}
				sql.append("'").append(r).append("' OR ");
			}
			sql.delete(sql.lastIndexOf("OR"), sql.length());
			sql.append(")");
		}
		return sql.toString();
	}

	public RegionDao getRegionDao() {
		return regionDao;
	}

	public void setRegionDao(RegionDao regionDao) {
		this.regionDao = regionDao;
	}

}
